package com.start.form.engine.core.global;

import com.start.common.std.StartEventDispatcherListener;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.form.engine.core.StartColumnInfo;
import com.start.form.engine.core.model.StartFormColumnModel;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.listener.StartTableListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Slf4j
public class DispatcherStartFormListener extends StartEventDispatcherListener<DispatcherStartFormListener, StartTableListener> implements StartTableListener {

    /**
     * 获取实例对象
     *
     * @return
     */
    public static DispatcherStartFormListener getInstance() {
        return StartSingletonFactory.get(DispatcherStartFormListener.class);
    }

    /**
     * 初始化分发器
     */
    @Override
    protected void initDispatcher() {
        Map<String, StartTableListener> beans = SpringContextTools.getBeans(StartTableListener.class, false);
        if (beans == null || beans.isEmpty()) {
            return;
        }
        Collection<StartTableListener> values = beans.values();
        for (StartTableListener listener : values) {
            if (StringUtils.isNotEmpty(listener.getTableName())) {
                addEvent(UNION_KEY_PREFIX, listener.getTableName(), listener);

                continue;
            }
            if (StringUtils.isNotEmpty(listener.getGroupCode())) {
                addEvent(GROUP_PREFIX, listener.getGroupCode(), listener);
                continue;
            }
            addEvent(GLOBAL_PREFIX, GLOBAL_KEY, listener);
        }
    }


    /**
     * 创建数据表前
     *
     * @param formConfig
     */
    @Override
    public void beforeCreateTable(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeCreateTable(formConfig);
            }
        }
    }

    /**
     * 创建表后
     *
     * @param formConfig
     */
    @Override
    public void afterCreateTable(StartFormConfig formConfig) {
        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterCreateTable(formConfig);
            }
        }
    }

    /**
     * 更新数据表前
     *
     * @param formConfig 表新配置
     * @param oldColumn  表旧列
     */
    @Override
    public void beforeUpdateTable(StartFormConfig formConfig, List<StartFormColumnModel> oldColumn) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeUpdateTable(formConfig, oldColumn);
            }
        }
    }

    /**
     * 更新数据表后
     *
     * @param formConfig
     */
    @Override
    public void afterUpdateTable(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterUpdateTable(formConfig);
            }
        }
    }

    /**
     * 同步数据表前
     *
     * @param formConfig      数据表配置
     * @param startColumnInfo
     */
    @Override
    public void beforeSynchronizationTable(StartFormConfig formConfig, StartColumnInfo startColumnInfo) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeSynchronizationTable(formConfig, startColumnInfo);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeSynchronizationTable(formConfig, startColumnInfo);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeSynchronizationTable(formConfig, startColumnInfo);
            }
        }
    }

    /**
     * 同步数据表后
     *
     * @param formConfig      数据表配置
     * @param newColumn
     * @param startColumnInfo
     */
    @Override
    public void afterSynchronizationTable(StartFormConfig formConfig, List<StartFormColumnModel> newColumn, StartColumnInfo startColumnInfo) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn,startColumnInfo);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn,startColumnInfo);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterSynchronizationTable(formConfig, newColumn, startColumnInfo);
            }
        }
    }

    /**
     * 删除数据表之前
     *
     * @param formConfig
     */
    @Override
    public void beforeDeleteTable(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeDeleteTable(formConfig);
            }
        }
    }

    /**
     * 删除数据表之后
     *
     * @param formConfig
     */
    @Override
    public void afterDeleteTable(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterDeleteTable(formConfig);
            }
        }
    }

    /**
     * 清空数据表前
     *
     * @param formConfig
     */
    @Override
    public void beforeClearData(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.beforeClearData(formConfig);
            }
        }
    }

    /**
     * 清口数据表后
     *
     * @param formConfig
     */
    @Override
    public void afterClearData(StartFormConfig formConfig) {

        List<StartTableListener> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        ;
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (StartTableListener listener : listeners) {
                listener.afterClearData(formConfig);
            }
        }
    }


}
